package com.tek.ding.faceding.network.rx.observable;

import android.content.Context;
import android.util.Log;

import com.tek.ding.faceding.network.exception.BaseException;
import com.tek.ding.faceding.network.rx.RxErrorHandles;

import java.util.Objects;

import io.reactivex.rxjava3.annotations.NonNull;

public abstract class ErrorHandleObservable<T> extends DefaultObservable<T> {

    private Context context;
    public RxErrorHandles rxErrorHandle;

    public ErrorHandleObservable(RxErrorHandles rxErrorHandle) {
        this.rxErrorHandle = rxErrorHandle;
    }

    public ErrorHandleObservable(Context context) {
        this.context = context;
        this.rxErrorHandle = new RxErrorHandles(context);
    }

    @Override
    public void onError(@NonNull Throwable e) {
        BaseException baseException = rxErrorHandle.handleError(e);
        if (baseException == null) {
            e.printStackTrace();
            Log.d("ErrorHandleObservable", Objects.requireNonNull(e.getMessage()));
        } else {
            //显示信息
            rxErrorHandle.showErrorMsg(baseException);
        }

    }
}
